package com.faselbaum.stalkme.activities;

import java.util.ArrayList;
import java.util.HashMap;

import com.faselbaum.stalkme.models.BluetoothDeviceInfo;
import com.faselbaum.stalkme.utils.BluetoothAccess;
import com.faselbaum.stalkme.R;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DevicesList extends Activity 
{
	
	private ListView _listView_buddyList;
	private ArrayList<String> _deviceList = new ArrayList<String>();
	private ArrayAdapter<String> _listAdapter = null;
	private EditText _editText_filter;
	private BluetoothAccess _btAccess = new BluetoothAccess(this);
	
	private int _scanInterval = 5000;
	private int _genericITL = 12;
	
	
	private Runnable _refreshRun = new Runnable()
	{
		
		@Override
		public void run()
		{
			// TODO Auto-generated method stub
			updateList();
		}
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.layout_devicelist);    	
    	
    	_listView_buddyList = (ListView) this.findViewById(R.id.listView_buddyList);
    	_editText_filter = (EditText) this.findViewById(R.id.editText_filterBox);
    	
    	_listView_buddyList.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				// TODO Auto-generated method stub
	    	    Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),
	    	    Toast.LENGTH_SHORT).show();
			}
		});
    	
    	_editText_filter.addTextChangedListener(new TextWatcher()
		{
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
				// TODO Auto-generated method stub
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				// TODO Auto-generated method stub
				String filterText = _editText_filter.getText().toString();
				if(filterText.length()==0)
				{
					_listView_buddyList.clearTextFilter();
					_listView_buddyList.setTextFilterEnabled(false);
				}
				else
				{
					if(_listView_buddyList.getTextFilter()==null) _listView_buddyList.setTextFilterEnabled(true);
					_listView_buddyList.setFilterText(filterText);
				}
			}
		});
    	
   	 	_listAdapter = new ArrayAdapter<String>(this, R.layout.list_item_device_info, _deviceList);
   	 	_listView_buddyList.setAdapter(_listAdapter);
   	 	
    	_btAccess.setGenericITL(_genericITL);
    	_btAccess.setOnUpdateRunnable(_refreshRun);
    	_btAccess.setScanInterval(_scanInterval);
    	
    }
    
    private void updateList()
    {
    	HashMap<String, BluetoothDeviceInfo> devices = _btAccess.getNearbyDevicesMap();
    	_deviceList.clear();
    	for(BluetoothDeviceInfo device : devices.values())
    	{
    		_deviceList.add(device.toString());
    	}
		_listAdapter.notifyDataSetChanged();
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_profiles_list, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
	    // Handle item selection
	    switch (item.getItemId()) 
	    {
	        case R.id.item_quit:
	        	this.finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    
    private void cleanUp()
    {
    	_btAccess.stopScanning();
    }
    
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    	cleanUp();
    }
    
}